package wqh.global;


import wqh.domain.Channel;

import java.util.ArrayList;
import java.util.List;

public class GlobalChannel {

    private static final List<Channel> channelList = new ArrayList<Channel>();

    public static synchronized void addChannel(Channel c)
    {
        if(!channelList.isEmpty()){
            for(Channel channel : channelList){
                if(channel.getId().intValue() == c.getId().intValue()){
                    continue;
                }else{
                    channelList.add(c);
                    break;
                }
            }
        }else{
            channelList.add(c);
        }
    }

    public static synchronized void setChannelList(List<Channel> cs)
    {
        channelList.clear();
        for(Channel channel : cs){
            channelList.add(channel);
        }
    }

    public static synchronized void removeChannel(Channel c)
    {
        if(c != null){
            for(Channel channel : channelList){
                if(channel.getId() == null ? c.getId() == null : channel.getId().equals(c.getId())){
                    channelList.remove(channel);
                    break;
                }
            }
        }
    }

    public static synchronized boolean existChannel(String ip, byte c)
    {
      if(ip != null){
          for(Channel channel : channelList){
              if(channel.getIp().equals(ip) && channel.getChannel() == c) return true;
          }
      }
        return false;
    }

    public static synchronized int count()
    {
        return channelList.size();
    }

    public static synchronized List<Channel> getChannelList()
    {
        return channelList;
    }

}
